﻿/**
 * @file launcher_quick_function.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_LAUNCHER2 != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/
#define LV_WATCH_LOWER_LIMIT_FOR_BATTERY_4  75
#define LV_WATCH_LOWER_LIMIT_FOR_BATTERY_3  50
#define LV_WATCH_LOWER_LIMIT_FOR_BATTERY_2  25
#define LV_WATCH_LOWER_LIMIT_FOR_BATTERY_1  0

/**********************
 *      TYPEDEFS
 **********************/
typedef struct {
    lv_cont_ext_t oldext;
    lv_obj_t   *   sim_signal_img;
    lv_obj_t   *   bt_img;
    lv_obj_t   *   wifi_signal_img;
    lv_obj_t   *   network_status_cont;
    lv_obj_t   *   charging_img;
    lv_obj_t   *   battery_img;
    lv_obj_t   *   battery_label;
    lv_obj_t   *   date_label;
    lv_obj_t   *   weekday_label;
} lv_launcher_quick_func_ext_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void launcher_quick_start_app_create(lv_obj_t * par);
static void launcher_quick_status_create(lv_obj_t * par);
/**********************
 *  STATIC VARIABLES
 **********************/
static watch_app_info_t quick_app[] = {
    { ICON_DIALPAD_GRAY, WATCH_TEXT_ID_DIALPAD, dialpad_create_event_cb },
    { ICON_CAMERA_GRAY, WATCH_TEXT_ID_CAMERA, camera_create_btn_event_cb },

    { ICON_CALCULATOR_GRAY, WATCH_TEXT_ID_CALCULATOR, calculator_create_event_cb },
    { ICON_ALARM_GRAY, WATCH_TEXT_ID_ALARM, alarm_create_event_cb },
    { ICON_SETTING_GRAY, WATCH_TEXT_ID_SETTINGS, setting_create_event_cb }
};
#if USE_LV_WATCH_STATUS_WIFI || USE_LV_WLAN
static const void * wifi_signal_img[] = { ICON_STATUS_WIFI_0, ICON_STATUS_WIFI_1,
    ICON_STATUS_WIFI_2, ICON_STATUS_WIFI_3,
    ICON_STATUS_WIFI_4
};
#endif
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void launcher_quick_function_create(lv_obj_t * par)
{
    launcher_quick_start_app_create(par);
    launcher_quick_status_create(par);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void quick_start_commont_event(lv_obj_t * btn, lv_event_t e)
{
    uint8_t index;

    if(LV_EVENT_CLICKED == e) {
        for(index = 0; index < sizeof(quick_app) / sizeof(quick_app[0]); index++) {
            if(quick_app[index].img_src == lv_imgbtn_get_src(btn, LV_BTN_STATE_RELEASED)) {
                quick_app[index].event_cb(btn, e);
                return;
            }
        }
    }

    if(LV_EVENT_PRESSED == e) {
        lv_signal_send(lv_obj_get_parent(lv_obj_get_parent(btn)), LV_SIGNAL_PRESSED, NULL);
    }
}

static void launcher_quick_start_app_create(lv_obj_t * par)
{
    uint8_t i = 0;
    uint8_t btn_w = 0;
    uint8_t cont_h = LV_HOR_RES * 7 / 10;
    lv_obj_t * btn;
    lv_obj_t * label;
    lv_obj_t * cont_top = lv_cont_create(par, NULL);
    lv_obj_set_style_local_bg_opa(cont_top, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_size(cont_top, LV_VER_RES, cont_h);
    lv_cont_set_layout(cont_top, LV_LAYOUT_OFF);
    lv_obj_align(cont_top, par, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_page_glue_obj(cont_top, true);
    for(i = 0; i < sizeof(quick_app) / sizeof(quick_app[0]); i++) {
        btn = lv_imgbtn_create(cont_top, NULL);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_RELEASED, quick_app[i].img_src);
        lv_imgbtn_set_src(btn, LV_BTN_STATE_PRESSED, quick_app[i].img_src);
        lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_30);
        lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
        lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_COVER);
        lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0x27, 0xB3, 0xF1));
        lv_obj_set_event_cb(btn, quick_start_commont_event);
        lv_page_glue_obj(btn, true);
        label = lv_label_create(cont_top, NULL);
        lv_label_set_text_id(label, quick_app[i].txtId);
        lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
        lv_obj_set_style_local_text_font(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_20);

        btn_w = lv_obj_get_width(btn);
        if(i < 2)
            lv_obj_align(btn, cont_top, LV_ALIGN_IN_TOP_LEFT, LV_VER_RES / 4 + (btn_w + LV_VER_RES / 10) * i,  LV_HOR_RES / 15);
        else
            lv_obj_align(btn, cont_top, LV_ALIGN_IN_BOTTOM_LEFT, LV_VER_RES / 9 + (btn_w + LV_VER_RES / 10) * (i - 2),  -LV_HOR_RES / 12);
        lv_obj_align(label, btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
    }
}

const char * launcher_quick_get_battery_img(uint8_t level)
{
    if(level > LV_WATCH_LOWER_LIMIT_FOR_BATTERY_4) {
        return(ICON_BATTERY_4);
    }

    if(level > LV_WATCH_LOWER_LIMIT_FOR_BATTERY_3) {
        return(ICON_BATTERY_3);
    }

    if(level > LV_WATCH_LOWER_LIMIT_FOR_BATTERY_2) {
        return(ICON_BATTERY_2);
    }

    if(level > LV_WATCH_LOWER_LIMIT_FOR_BATTERY_1) {
        return(ICON_BATTERY_1);
    }

    return(ICON_STATUS_BATTERY_0);
}

static void launcher_quick_status_update(lv_anim_t * anim)
{
    lv_obj_t * content = (lv_obj_t *)anim->var;
    lv_obj_t * img;
    char str[20] = {};
    lv_launcher_quick_func_ext_t * status_ext = (lv_launcher_quick_func_ext_t *)lv_obj_get_ext_attr(content);
    const char * phone_signal_img[] = { ICON_STATUS_SIGNAL_0, ICON_STATUS_SIGNAL_1,
                                        ICON_STATUS_SIGNAL_2, ICON_STATUS_SIGNAL_3,
                                        ICON_STATUS_SIGNAL_4, ICON_STATUS_SIGNAL_4
                                      };
    // update wifi signal
    bool is_wifi_connected = false;
#if USE_LV_WATCH_STATUS_WIFI || USE_LV_WLAN
#if USE_LV_WLAN == 0
    lv_img_set_src(status_ext->wifi_signal_img, wifi_signal_img[get_wifi()]);
#else
    HAL_WLAN_SIGNAL_STRENGTH_T act_ap_rssi_level = hal_wlan_get_rssi_level();
    printf("%s: wifi rssi level %d\n", __FUNCTION__, act_ap_rssi_level);
    if(HAL_WLAN_SIGNAL_STRENGTH_NO < act_ap_rssi_level) {
        is_wifi_connected = true;
        if(lv_obj_get_hidden(status_ext->wifi_signal_img)) {
            lv_obj_set_hidden(status_ext->wifi_signal_img, false);
        }
        lv_img_set_src(status_ext->wifi_signal_img, wifi_signal_img[act_ap_rssi_level]);
    } else if(false == lv_obj_get_hidden(status_ext->wifi_signal_img)) {
        lv_obj_set_hidden(status_ext->wifi_signal_img, true);
    }
#endif
#endif

    /*update network status and phone signal*/
    lv_obj_clean(status_ext->network_status_cont);
    if(true == watch_modem_sim_present_check_req()) {
        MMI_MODEM_PLMN_RAT rat;
        watch_modem_get_operator_req(&rat);

        /* 2G/4G icon */
        lv_obj_t * label = lv_label_create(status_ext->network_status_cont, NULL);
        lv_obj_set_style_local_text_font(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_20);
        if(MMI_MODEM_PLMN_RAT_LTE == rat) {
            lv_label_set_text(label, "4G");
            lv_img_set_src(status_ext->sim_signal_img, phone_signal_img[watch_modem_get_signal_bar_req()]);
            if(!is_wifi_connected) {
                /* IMS register state */
                uint8_t ims_reg_state = watch_modem_get_ims_reg_state_req();
                if(1 == ims_reg_state) {
                    lv_obj_t * label_ims = lv_label_create(status_ext->network_status_cont, NULL);
                    lv_obj_set_style_local_text_font(label_ims, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_12);
                    lv_obj_align(label_ims, label, LV_ALIGN_OUT_RIGHT_TOP, 5, 0);
                    lv_label_set_text(label_ims, "HD");
                    lv_obj_set_click(label_ims, false);
                }
            }
        } else if(MMI_MODEM_PLMN_RAT_UMTS == rat) {
            lv_label_set_text(label, "3G");
            lv_img_set_src(status_ext->sim_signal_img, phone_signal_img[watch_modem_get_signal_bar_req()]);
            if(!is_wifi_connected) {
                /* UMTS register state */
                uint8_t umts_reg_state = watch_modem_get_umts_reg_state_req();
                lv_obj_t * label_umts = lv_label_create(status_ext->network_status_cont, NULL);
                lv_obj_set_style_local_text_font(label_umts, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_12);
                lv_obj_align(label_umts, label, LV_ALIGN_OUT_RIGHT_TOP, 5, 0);
                if(0 == umts_reg_state) {
                    lv_label_set_text(label_umts, "W");
                } else if(1 == umts_reg_state) {
                    lv_label_set_text(label_umts, "TD");
                } else if(2 == umts_reg_state) {
                    lv_label_set_text(label_umts, "H");
                }
            }
        } else if(MMI_MODEM_PLMN_RAT_GSM == rat) {
            lv_label_set_text(label, "2G");
            lv_img_set_src(status_ext->sim_signal_img, phone_signal_img[watch_modem_get_signal_bar_req()]);
            if(!is_wifi_connected) {
                /* GSM register state */
                uint8_t gsm_reg_state = watch_modem_get_gsm_reg_state_req();
                if(1 == gsm_reg_state) {
                    lv_obj_t * label_gsm = lv_label_create(status_ext->network_status_cont, NULL);
                    lv_obj_set_style_local_text_font(label_gsm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_12);
                    lv_obj_align(label_gsm, label, LV_ALIGN_OUT_RIGHT_TOP, 5, 0);
                    lv_label_set_text(label_gsm, "E");
                    lv_obj_set_click(label_gsm, false);
                }
            }
        } else {
            lv_label_set_text(label, "");
            lv_img_set_src(status_ext->sim_signal_img, phone_signal_img[0]);
        }
        lv_obj_set_click(label, false);

    } else {
        img = lv_img_create(status_ext->network_status_cont, NULL);
        LV_ASSERT_MEM(img);
        if(img == NULL) return;
        lv_img_set_src(img, ICON_STATUS_NO_SIM_CARD);
        lv_img_set_src(status_ext->sim_signal_img, phone_signal_img[0]);
    }

    // update charging status
    if(HAL_CHG_CONNECTED == Hal_Charger_Get_Status()) {
        lv_img_set_src(status_ext->battery_img, ICON_CHARGING);
        if(Hal_Battery_Get_Status() == 100) {
            lv_img_set_src(status_ext->battery_img, ICON_CHARGING_OK);
        }
    } else {
        lv_img_set_src(status_ext->battery_img, launcher_quick_get_battery_img(Hal_Battery_Get_Status()));
    }
    snprintf(str, 20, "%d%%", Hal_Battery_Get_Status());
    lv_label_set_text(status_ext->battery_label, str);

    // update time
    hal_rtc_t tm;
    const uint16_t week_text_id[] = {WATCH_TEXT_ID_SUNDAY,
                                     WATCH_TEXT_ID_MONDAY,
                                     WATCH_TEXT_ID_TUESDAY,
                                     WATCH_TEXT_ID_WEDNESDAY,
                                     WATCH_TEXT_ID_THURSDAY,
                                     WATCH_TEXT_ID_FRIDAY,
                                     WATCH_TEXT_ID_SATURDAY
                                    };

    Hal_Rtc_Gettime(&tm);
    if(LANG_EN == lv_lang_act()) {
        snprintf(str, 20, "%.2d-%.2d", tm.tm_mon, tm.tm_mday);
    } else {
        snprintf(str, 20, "%.2d%s%.2d%s", tm.tm_mon, (char *)lv_lang_get_text(WATCH_TEXT_ID_MONTH), tm.tm_mday, (char *)lv_lang_get_text(WATCH_TEXT_ID_DATE));
    }
    lv_label_set_text(status_ext->date_label, str);
    lv_label_set_text_id(status_ext->weekday_label, week_text_id[tm.tm_wday]);
}

static void launcher_quick_status_update_start(lv_obj_t * cont)
{
    lv_anim_t a;

    lv_anim_init(&a);
    lv_anim_set_var(&a, cont);
    lv_anim_set_time(&a, 1);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 1000);       // update every 1s
    lv_anim_set_start_cb(&a, launcher_quick_status_update);

    lv_anim_start(&a);
    launcher_quick_status_update(&a);
}

static void launcher_quick_status_create(lv_obj_t * par)
{
    lv_obj_t * cont_bottom = lv_cont_create(par, NULL);
    lv_obj_set_style_local_bg_opa(cont_bottom, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_size(cont_bottom, LV_VER_RES, LV_HOR_RES * 3 / 10);
    lv_obj_align(cont_bottom, par, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_obj_set_click(cont_bottom, false);
    lv_cont_set_layout(cont_bottom, LV_LAYOUT_OFF);
    lv_launcher_quick_func_ext_t * ext = (lv_launcher_quick_func_ext_t *)lv_obj_allocate_ext_attr(cont_bottom, sizeof(lv_launcher_quick_func_ext_t));

    lv_obj_t * cont1 = lv_cont_create(cont_bottom, NULL);
    lv_obj_set_size(cont1, LV_VER_RES, LV_HOR_RES / 10);
    lv_obj_set_style_local_bg_opa(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_align(cont1, cont_bottom, LV_ALIGN_IN_TOP_MID, 0, LV_HOR_RES / 24);
    lv_obj_set_click(cont1, false);
    lv_cont_set_layout(cont1, LV_LAYOUT_ROW_MID);

    /*sim signal*/
    ext->sim_signal_img = lv_img_create(cont1, NULL);

    /*newwork*/
    ext->network_status_cont = lv_cont_create(cont1, cont_bottom);
    lv_obj_set_size(ext->network_status_cont, LV_VER_RES / 7, lv_obj_get_height(ext->sim_signal_img));
    lv_obj_set_click(ext->network_status_cont, false);

    /*bt*/
#if USE_LV_BLUETOOTH
    ext->bt_img = lv_img_create(cont1, NULL);
    if(bt_get_state()) lv_img_set_src(ext->bt_img, ICON_BT_ON);
    else lv_img_set_src(ext->bt_img, ICON_BT_OFF);
#endif

    /*wifi*/
#if USE_LV_WATCH_STATUS_WIFI || USE_LV_WLAN
    ext->wifi_signal_img = lv_img_create(cont1, NULL);
    lv_img_set_src(ext->wifi_signal_img, wifi_signal_img[4]);
#endif

    /*battery*/
    ext->battery_img = lv_img_create(cont1, NULL);
    ext->battery_label = lv_label_create(cont1, NULL);
    lv_obj_set_style_local_text_font(ext->battery_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_20);
    lv_obj_set_click(ext->battery_label, false);

    uint8_t child_cnt = lv_obj_count_children(cont1);
    lv_obj_set_style_local_pad_left(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_VER_RES / (child_cnt + 1));
    lv_obj_set_style_local_pad_inner(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_VER_RES / (child_cnt * child_cnt));

    lv_obj_t * cont2 = lv_cont_create(cont_bottom, cont1);
    lv_obj_set_style_local_pad_left(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_VER_RES / 3);
    lv_obj_set_style_local_pad_inner(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_VER_RES / 20);
    lv_obj_align(cont2, cont1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

    /*date*/
    ext->date_label = lv_label_create(cont2, NULL);
    lv_obj_set_style_local_text_font(ext->date_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_20);
    lv_obj_set_click(ext->date_label, false);

    /*week*/
    ext->weekday_label = lv_label_create(cont2, ext->date_label);

    launcher_quick_status_update_start(cont_bottom);
}
#endif /*USE_LV_WATCH_LAUNCHER2*/
